package org.fund.common.util;

import cn.hutool.http.HttpUtil;
import org.fund.common.constant.HolidayEnum;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:
 * @Date: 2022/3/15 16:01
 * @Description:
 */
public class DateUtils {
    public static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    public static final String dateUrl = "http://tool.bitefu.net/jiari/";
    public static final Map<String, Integer> map = new HashMap<>();
    public static  Date before =new Date();
    static {
        map.put("2022-01-01",1);
        map.put("2022-01-02",1);
        map.put("2022-01-03",1);
        map.put("2022-01-31",1);
        map.put("2022-02-01",1);
        map.put("2022-02-02",1);
        map.put("2022-02-03",1);
        map.put("2022-02-04",1);
        map.put("2022-02-05",1);
        map.put("2022-02-06",1);
        map.put("2022-02-12",1);
        map.put("2022-02-13",1);
        map.put("2022-02-19",1);
        map.put("2022-02-20",1);
        map.put("2022-02-26",1);
        map.put("2022-02-27",1);
        map.put("2022-03-05",1);
        map.put("2022-03-12",1);
        map.put("2022-03-13",1);
        map.put("2022-03-19",1);
        map.put("2022-03-20",1);
        map.put("2022-03-26",1);
        map.put("2022-03-27",1);
        map.put("2022-04-03",1);
        map.put("2022-04-04",1);
        map.put("2022-04-05",1);
        map.put("2022-04-09",1);
        map.put("2022-04-10",1);
        map.put("2022-04-16",1);
        map.put("2022-04-23",1);
        map.put("2022-04-24",1);
        map.put("2022-05-08",1);
        map.put("2022-05-07",1);
        map.put("2022-05-14",1);
        map.put("2022-05-15",1);
        map.put("2022-05-22",1);
        map.put("2022-05-21",1);
        map.put("2022-05-28",1);
        map.put("2022-05-29",1);
        map.put("2022-04-17",1);
        map.put("2022-04-30",1);
        map.put("2022-05-01",1);
        map.put("2022-05-02",1);
        map.put("2022-05-03",1);
        map.put("2022-05-04",1);
        map.put("2022-06-03",1);
        map.put("2022-06-04",1);
        map.put("2022-06-05",1);
        map.put("2022-06-12",1);
        map.put("2022-06-11",1);
        map.put("2022-06-19",1);
        map.put("2022-06-18",1);
        map.put("2022-06-25",1);
        map.put("2022-06-26",1);
        map.put("2022-07-02",1);
        map.put("2022-07-03",1);
    }
    public static boolean dayBefore(Date target) throws ParseException {
        Date current = new Date();
        return getDate(target).before(getDate(current));
    }

    public static Date getDate(Date date) throws ParseException {
        String format = df.format(date);
        return df.parse(format);
    }
    public static int isHolidayAndWeekend(String date){
        if (map.containsKey(date)){
            return 0;
        }
        return 1;
    }
    public  static HolidayEnum isHolidayAndWeekend(Date date){
        return HolidayEnum.getInstance(isHolidayAndWeekend(df.format(date)));
    }
    public static String getWorkDay(Date date){
        if (!isHolidayAndWeekend(date).equals(HolidayEnum.COMMON)){
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.add(Calendar.DATE, -1);
            date = ca.getTime();
            return getWorkDay(date);
        }
        return df.format(date);
    }

    /**
     * 获取多少工作日之前的日期
     * @param date
     * @param count
     * @return
     * @throws ParseException
     */
   public static String getDay(String date,int count) throws ParseException {
       Date dNow = df.parse(date);
       Date before = null;
       while (count!=1){
           before = getBefore(dNow);
            if (isHolidayAndWeekend(dNow).getCode()==1||isHolidayAndWeekend(dNow).getCode()==2){
                dNow = before;
            }else {
                dNow = before;
                count--;
            }
       }
       return df.format(dNow);
   }

    /**
     *
     * @param date
     * @return
     * @throws ParseException
     */
   private static Date getBefore(Date date) throws ParseException {
       Calendar calendar = Calendar.getInstance(); //得到日历
       calendar.setTime(date);//把当前时间赋给日历
       calendar.add(Calendar.DAY_OF_WEEK, -1);  //设置为前一天
       String format = df.format(calendar.getTime());//得到前一天的时间
       return df.parse(format);
   }
   public static String getDayBefore(){
       Date date = new Date();
       Calendar calendar = Calendar.getInstance(); //得到日历
       calendar.setTime(date);//把当前时间赋给日历
       calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
       before= calendar.getTime();
       return df.format(before);
   }
   public static String getBeforeYesterday(){
       Date date = new Date();
       Calendar calendar = Calendar.getInstance(); //得到日历
       calendar.setTime(date);//把当前时间赋给日历
       calendar.add(Calendar.DAY_OF_MONTH, -2);  //设置为前天
       before= calendar.getTime();
       String format = df.format(before);
       if (isHolidayAndWeekend(format)==0){
           format = getWorkDay(before);
       }
       return format;
   }

   public static String getCurrentDay(){
       return df.format(new Date());
   }
    /**
     * 判断是否是周一
     *
     * @param date
     */
    public static boolean isMonday(String date) {
        try {
            Date parse = df.parse(date);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            return cal.get(Calendar.DAY_OF_WEEK) - 1 == 1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
